package wcg;

import ikelib.byteUtil;
import ikelib.unsigned_byte;
import ikelib.wintime;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.net.DatagramPacket;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Set;
import java.util.TreeMap;
import java.util.TreeSet;


public class winUdp2File {
	
	private winStream WinStream = new winStream();
	private wintime  CurWinTime = new wintime();
	private String  Path = new String();
	
	public winUdp2File(){
		
	}
	public void setPath(String p){
		Path = p;
		if(Path.isEmpty()) return;
		//if(Path.charAt(Path.length()-1) != '\\'){
		if(Path.charAt(Path.length()-1) != File.separatorChar){
			//Path += '\\';
			Path += File.separator;
		}
	}
	public String getPath(){
		return Path;
	}
	private void writeFileOld(){
		wintime wtime = new wintime(CurWinTime);
		wtime.dec_min();
		wtime.sub_time(0,0,wtime.sec(),wtime.msec());
		String fname2 = String.format("%s%02d%02d%02d%02d.%02d", Path,wtime.year()-2000,wtime.month(),wtime.day(),wtime.hour(),wtime.min());
		String fname = String.format("%s%s.%s", Path,"tmp","tmp");
		int good = 0;
		try {
			FileOutputStream wfile = new FileOutputStream(fname);
			for(int i=0;i<60;i++,wtime.inc_sec()){
				int index = 0;
				byte[]  data  = new byte[1024*8*100];
				byte[] tm = wtime.getBcd();
				for(int j=0;j<tm.length;j++){
					data[index++] = tm[j];
				}
				int[] chary = WinStream.getChannel();
				int chcount=0;
				for(int j=0;j<chary.length;j++){
					channelRawData rd = WinStream.getChannelRawData(wtime,chary[j]);
					if(rd != null){
						chcount++;
						byte[] rdd = rd.getChannelRawData();
						if(rdd.length>=(1024*8)){
							System.err.println("Data size too large");
						}
						for(int k=0;k<rdd.length;k++){
							data[index++] = rdd[k];
						}
						WinStream.removeChannelRawData(wtime,chary[j]);
						//System.out.println(String.format("File Write ch=%04x,inx=%d,len=%d",chary[j],index,rdd.length));
					}
				}
				int length = index;
				if(length > 6){
					byte[] len = byteUtil.conv4UByte(length+4);
					wfile.write(len);
					wfile.write(data,0, length);
					good = 1;
				}
				//System.out.println(String.format("-- File write sec=%d len=%d",i,length));
			}
			wfile.close();
		} catch (IOException e) {
			e.printStackTrace();
			return;
		} 
		if(good != 0){
			File fsor = new File(fname);
			File fdes = new File(fname2);
			fsor.renameTo(fdes);
		}
	}
//	public void add(DatagramPacket pac){
//		WinStream.add(pac);
//		wintime ctm = WinStream.getLastTime();
//		if(ctm.sec() == 10 && CurWinTime.compare(ctm)!=0){
//			CurWinTime = ctm;
//			writeFile();
//		}
//	}
	/*
	 * 従来のファイル書き込み
	 */
	private void addPacOld(byte[] pac){
		if(ActProtocolFrame.isActHeader(pac,0)){
			pac = ActProtocolFrame.convWinA0packet(pac,0);
		}
		WinStream.add(pac);
		wintime ctm = WinStream.getLastTime();
		if(ctm.sec() == 30 && CurWinTime.compare(ctm)!=0){
			CurWinTime = ctm;
			writeFile();
		}
	}
	/**
	 * 新型の書き込み
	 * @param pac
	 */
	private void addPac(byte[] pac){
		if(ActProtocolFrame.isActHeader(pac,0)){
			pac = ActProtocolFrame.convWinA0packet(pac,0);
		}
		// TODO:FIX TP8800でACT受信でFile書き込みのとき例外が発生する問題
		WinStream.add(pac);
		writeFile(false);
	}
	/**
	 * 
	 */
	long LastWriteTime = 0L;
	/**
	 * ファイルへの書き出しの入り口
	 * @param forcewrite　
	 * 　　強制的に書き込む
	 */
	public void writeFile(boolean forcewrite){
		if(LastWriteTime==0){
			LastWriteTime = System.currentTimeMillis();
		}
		if(forcewrite){
			LastWriteTime = System.currentTimeMillis();
			writeFile();
			return;
		}
		long curtime = System.currentTimeMillis();
		if(curtime - LastWriteTime > 5000L){
			LastWriteTime = curtime;
			writeFile();
		}
	}
	/**
	 * ファイルへの書き出し
	 */
	private void writeFile(){
		
		int[] chary = WinStream.getChannel();
		if(chary.length<=0) return;
		//System.out.println(String.format("start write file %dsec chnum=%d",secnum,chary.length));
		TreeMap<wintime,ArrayList<channelRawData>> map  =new TreeMap<wintime,ArrayList<channelRawData>>();
		TreeSet<wintime> ts = WinStream.TimeSet;
		Iterator<wintime> ite = ts.iterator();
		wintime wt = null;
		wintime wtmsk2 = null;
		while(ite.hasNext()){
			wt = ite.next();
			wintime wtmsk1 = new wintime(wt.year(),wt.month(),wt.day(),wt.hour(),wt.min(),0);
			// 1秒分のデータを集める
			ArrayList<channelRawData> datalist = new ArrayList<channelRawData>();
			for(int j=0;j<chary.length;j++){
				channelRawData rd = WinStream.getChannelRawData(wt,chary[j]);
				if(rd == null) continue;
				//System.out.println(String.format("write buf add %s, ch=%d,len=%d",wt.fmt(),chary[j],rd.DataLength));
				datalist.add(rd);
			}
			//System.out.println("write buf put " + wt.fmt());
			if(wtmsk2==null){
				wtmsk2 = new wintime(wtmsk1);
				map.put(new wintime(wt), datalist);
				continue;
			}
			if(wtmsk1.compare(wtmsk2)==0){
				map.put(new wintime(wt), datalist);
				continue;
			}
			// ファイルに書き込んで
			String fname = String.format("%s%02d%02d%02d%02d.%02d", Path,wtmsk2.year()%100,wtmsk2.month(),wtmsk2.day(),wtmsk2.hour(),wtmsk2.min());
			try {
				margeWriteFile(fname,map);
			} catch (IOException e) {
				// e.printStackTrace();
				//System.err.println("Win file output error : "+fname);
			}
			map.clear();
			// クリア―して
			
			wtmsk2 = new wintime(wtmsk1);
			map.put(new wintime(wt), datalist);
		}
		
		String fname = String.format("%s%02d%02d%02d%02d.%02d", Path,wt.year()%100,wt.month(),wt.day(),wt.hour(),wt.min());
		try {
			margeWriteFile(fname,map);
		} catch (IOException e) {
			// e.printStackTrace();
			//System.err.println("Win file output error : "+fname);
		}
		map.clear();
		WinStream.clear();
		return;
	}
	/*
	 * winファイルにマージする
	 * ファイルがなければ新規に作成
	 * （マージするデータは指定のファイルにすべて格納されることが前提）
	 */
	private void margeWriteFile(String fname, TreeMap<wintime,ArrayList<channelRawData>> datalist) throws IOException {
		//System.out.println("marge&write : " + fname);
		File f1 = new File(fname);
		TreeMap<wintime,ArrayList<channelRawData>> map  =new TreeMap<wintime,ArrayList<channelRawData>>();
		if(f1.exists()){
			//System.out.println(" exist file : " + fname);
			FileInputStream ifile = new FileInputStream(fname);
			int num = ifile.available();
			byte[] dat = new byte[num];
			ifile.read(dat);
			ifile.close();
			// ファイルのデータを読み込みして変換する
			winFile wfile = new winFile(dat);
			map = wfile.getTreeMap();
			// マージする
			datalist = margeData(datalist,map);
		}
		FileOutputStream ofile=null;
		try{
			// Macだとアプリケーションフォルダー以下でないとFileNotFoundExceptionが発生する  
			ofile = new FileOutputStream(fname);
		} catch(FileNotFoundException e){
			//System.err.println(e);
		}catch(SecurityException e){
			//System.err.println(e);
		}
		
		Set<wintime> tmset = datalist.keySet();
		Iterator<wintime> ite = tmset.iterator();
		while(ite.hasNext()){
			wintime wt = ite.next();
			ArrayList<channelRawData> cdlist = datalist.get(wt);
			// サイズを求める
			int len = 0;
			for(int i=0;i<cdlist.size();i++){
				channelRawData dat = cdlist.get(i);
				if(dat==null) continue;
				len += dat.getChannelRawData().length;
			}
			if(len==0){
				continue;
			}
			// 書き込みバッファを確保して
			byte[] rdata = new byte[len + 4+6];
			// 秒ブロック長
			byte[] blklen = byteUtil.conv4UByte(len+4+6);
			int index = 0;
			System.arraycopy(blklen, 0, rdata, index, blklen.length);
			index += blklen.length;
			// BCD（時刻ヘッダ）
			byte[] bcd = wt.getBcd();
			System.arraycopy(bcd, 0, rdata, index, bcd.length);
			index += bcd.length;
			//System.out.println(String.format("write buf %s , %d",wt.fmt(),len));
			// チャネルデータセット
			for(int i=0;i<cdlist.size();i++){
				channelRawData dat = cdlist.get(i);
				if(dat==null){
					continue;
				}
				byte[] rdat = dat.getChannelRawData();
				System.arraycopy(rdat, 0, rdata, index, rdat.length);
				index += rdat.length;
				//System.out.println(String.format("write buf ch=%d , %d", dat.getChannelName(),index));
			}
			if(ofile!=null){
				ofile.write(rdata);
			}
			//System.out.println(String.format("write file %dbyte : %s\n", rdata.length,wt.fmt()));
		}
		if(ofile!=null){
			ofile.close();
		}
		
	}
	
	/**
	 * MAP同士をマージする
	 * @param d1
	 * @param d2
	 * @return
	 */
	private TreeMap<wintime,ArrayList<channelRawData>> margeData(TreeMap<wintime,ArrayList<channelRawData>> d1,TreeMap<wintime,ArrayList<channelRawData>> d2){
		TreeMap<wintime,ArrayList<channelRawData>> ret  =new TreeMap<wintime,ArrayList<channelRawData>>();
		Set<wintime> ks1 = d1.keySet();
		Set<wintime> ks2 = d2.keySet();
		
		TreeSet<wintime> key = new TreeSet<wintime>();
		
		Iterator<wintime> ite1 = ks1.iterator();
		while(ite1.hasNext()){
			wintime wt = ite1.next();
			key.add(new wintime(wt));
			//System.out.println("Marge 1 time="+wt.fmt());
		}
		Iterator<wintime> ite2 = ks2.iterator();
		while(ite2.hasNext()){
			wintime wt = ite2.next();
			key.add(new wintime(wt));
			//System.out.println("Marge 2 time="+wt.fmt());
		}
		
		Iterator<wintime> ite = key.iterator();
		while(ite.hasNext()){
			wintime wt = ite.next();
			ArrayList<channelRawData> ar1 = d1.get(wt);
			ArrayList<channelRawData> ar2 = d2.get(wt);
			ArrayList<channelRawData> ar = new ArrayList<channelRawData>();
			if(ar1 !=null){
				Iterator<channelRawData> itedata = ar1.iterator();
				while(itedata.hasNext()){
					channelRawData cdd = itedata.next();
					ar.add(cdd);
					//System.out.println("Marge 1  :"+ wt.fmt() + "  ch=" + cdd.ChannelName);
				}
			}
			if(ar2 != null){
				Iterator<channelRawData> itedata = ar2.iterator();
				while(itedata.hasNext()){
					channelRawData cdd = itedata.next();
					ar.add(cdd);
					//System.out.println("Marge 2  :"+ wt.fmt() + "  ch=" + cdd.ChannelName);
				}
			}
			//System.out.println("Marge put : "+ wt.fmt()+ " : " + ar.size()+"items" );
			ret.put(new wintime(wt),ar);
		}
		return ret;
	}
	/**
	 * win ファイルを表すクラス
	 * @author ikechan
	 *
	 */
	public class winFile{
		private int length;
		private wintime time;
		private channelRawData chdata;
		public TreeSet<wintime>    TimeSet = new TreeSet<wintime>();
		// 時刻 （秒）とchannelRawDataのMAP
		public TreeMap<wintime,ArrayList<channelRawData>>	ChannelDataSet = new TreeMap<wintime,ArrayList<channelRawData>>();
		winFile(byte[] d){
			for(int index = 0;index<d.length;index+=length){
				length = byteUtil.valueOf4Byte(d,0+index);
				time = new wintime(d,4+index);
				//System.out.println(String.format("read file : %s",time.fmt()));
				TimeSet.add(time);
				ArrayList<channelRawData> rdlist = new ArrayList<channelRawData>();
				for(int i=10;i<length;){
					chdata = new channelRawData(d,index+i);
					rdlist.add(chdata);
					i += chdata.RawData.length;
					//System.out.println(String.format("read file ch=%d,%d",chdata.getChannelName(),i));
				}
				ChannelDataSet.put(time,rdlist);
			}
		}

		// データを得る
		public TreeMap<wintime,ArrayList<channelRawData>> getTreeMap(){
			return ChannelDataSet;
		}
		
		
	}

	
	/**
	 * 受信したUDPパケットの内容をファイルを書き込む
	 * @param pac
	 */
	public void add(byte[] pac){
		//addPacOld(pac);
		addPac(pac);
	}
	/**
	 * winチャネルの生データ
	 * 時刻の次のwin チャネルヘッダからデータまでのデータ表す
	 * @author ikechan
	 *
	 */
	public class channelRawData{
		private byte[] RawData;
		private int ChannelName;
		private int samplerate;
		private int encode;
		//private int[] Data;
		private int DataLength;
		
		channelRawData(byte[] c, int offset){
			int index = offset;
			// channel header decode
			ChannelName = unsigned_byte.convShort(c[index])*0x100 + unsigned_byte.convShort(c[index+1]);
			encode = (0xf0 & (unsigned_byte.convShort(c[index+2])))>>4;
			samplerate = 0xfff & (unsigned_byte.convShort(c[index+2])*0x100 + unsigned_byte.convShort(c[index+3]));
			index += 4;
			// how many data  
			//int dataLength;
			if(encode == 0){	// 0.5byte 
				DataLength = 4 + samplerate / 2;
			}
			else{	// 1byte,2byte,3byte,4byte
				DataLength = 4 + (samplerate-1) * encode;
			}
			RawData = new byte[DataLength+4];
			for(int i=0;i<RawData.length;i++){
				if(offset+i >= c.length){
					//TODO: ACTパケット受信のときに ArrayBoundエラーになる問題がある
					System.out.print(String.format("WinRawDataLength %d %d",offset+i,c.length));
				}
				else{
					RawData[i] = c[offset+i];
				}
			}
		}
		public byte[] getChannelRawData(){
			return RawData;
		}
		public int getChannelName(){
			return ChannelName;
		}
		
	}
	/**
	 * win 秒チャンクを時刻別、ｃｈ別に保持するクラス
	 * @author ikechan
	 *
	 */
	public class winStream{
		private TreeMap<Integer,TreeMap<Integer,channelRawData>> TimeChunk = new TreeMap<Integer,TreeMap<Integer,channelRawData>>();
		//private TreeMap<Integer,channelData> ChannelChunk = new TreeMap<Integer,channelData>();
		public TreeSet<wintime>    TimeSet = new TreeSet<wintime>();
		private TreeSet<Integer>	ChannelSet = new TreeSet<Integer>();

	
		public void clear(){
			TimeChunk.clear();
			TimeSet.clear();
			ChannelSet.clear();
		}
		public void add(DatagramPacket pac){
			//int len  = pac.getLength();
			byte[] d = pac.getData();
			add(d);
		}
		public void add(byte[] pac){
//			byte[] d = pac;
//			winA0frame frame = new winA0frame(d);
//			{
//				int chname = frame.getChannelName();
//				channelRawData data = frame.getChannelRawData();
//				byte[] datab = data.getChannelRawData();
//				// System.out.println(String.format("Ch=%04x,len=%d", chname,datab.length));
//			}
//			int timekey = frame.getWinTimeKey();
//			TreeMap<Integer,channelRawData> ch=TimeChunk.get(timekey);
//			if(ch == null){
//				ch = new TreeMap<Integer,channelRawData>();
//			}
//			int channelName = frame.getChannelName();
//			ch.put(channelName,frame.getChannelRawData());
//			
//			
//			TimeChunk.put(timekey, ch);
//			wintime tm = new wintime(frame.getWinTime());
//			TimeSet.add(tm);
//			ChannelSet.add(channelName);
//			//System.out.println(String.format("Put > ch=%04x,time=%s %d",channelName,tm.fmt(),timekey));
			
			byte[] d = pac;
			winA0frame frame = new winA0frame(d);
			// TODO:FIX TP8800でACT受信でFile書き込みのとき例外が発生する問題
			if(frame.getA0() == -88 || frame.getA0() == -87 ) 
				return;
			int[] chname = frame.getChannelName();
			channelRawData[] data = frame.getChannelRawData();
			//byte[] datab = data.getChannelRawData();
			// System.out.println(String.format("Ch=%04x,len=%d", chname,datab.length));
			
			for(int i=0;i<chname.length;i++){
				int timekey = frame.getWinTimeKey();
				TreeMap<Integer,channelRawData> ch=TimeChunk.get(timekey);
				if(ch == null){
					ch = new TreeMap<Integer,channelRawData>();
				}
				int channelName = chname[i];
				
				ch.put(channelName,data[i]);
				TimeChunk.put(timekey, ch);
				wintime tm = new wintime(frame.getWinTime());
				TimeSet.add(tm);
				ChannelSet.add(channelName);
				//System.out.println(String.format("Put > ch=%04x,time=%s %d",channelName,tm.fmt(),timekey));
			}
		}
				
		public int getRemainSec(){
			return TimeSet.size();
		}
		public wintime getFirstTime(){
			return TimeSet.first();
		}
		public void removeFirstTime(){
			wintime tm = TimeSet.first();
			TimeSet.remove(tm);
		}
		public wintime getLastTime(){
			return TimeSet.last();
		}
		public int getFirstChannel(){
			return ChannelSet.first();
		}
		public int getLastChannel(){
			return ChannelSet.last();
		}
		public int getChannelSize(){
			return ChannelSet.size();
		}
		public int[] getChannel(){
			int[] ret = new int[ChannelSet.size()];
			Iterator<Integer> ite = ChannelSet.iterator();
			for(int i=0;ite.hasNext();i++){
				ret[i] = ite.next();
			}
			return ret;
		}
		public channelRawData getChannelRawData(wintime tm,int channel){
			int tmt = tm.getValueS();
			TreeMap<Integer,channelRawData> ch=TimeChunk.get(tmt);
			if(ch == null){
				return null;
			}
			channelRawData da = ch.get(channel);
			return da;
		}
		public void removeChannelRawData(wintime tm,int channel){
			int tmt = tm.getValueS();
			TreeMap<Integer,channelRawData> ch=TimeChunk.get(tmt);
			if(ch != null){
				ch.remove(channel);
				//TimeChunk.remove(tmt);
			}
		}
		public void removeTimeChunk(wintime tm){
			int tmt = tm.getValueS();
			TreeMap<Integer,channelRawData> ch=TimeChunk.get(tmt);
			if(ch != null){
				TimeChunk.remove(tmt);
			}
		}
		/**
		 * win A0フレームをあらわすクラス
		 * 1フレームに１ｃｈのみが格納されている→秒ブロックで複数CH格納に対応（ただしこれはwin仕様にない）
		 * もうひとつの形式のパケット長以降が複数含まれる形式には対応していない。
		 * @author ikechan
		 *
		 */
		public class winA0frame {
//			private int seq;
//			private int subseq;
			private int fmt;
			//private int paclen;
			private wintime time;
			//private channelRawData chdata;
			private ArrayList<channelRawData> chdata = new ArrayList<channelRawData>();
			winA0frame(byte[] d){
				int dlen = d.length;
				//seq = byteUtil.valueOf1Byte(d, 0);
				//subseq = byteUtil.valueOf1Byte(d, 1);
				fmt = byteUtil.valueOf1Byte(d, 2);
				// TODO:FIX TP8800でACT受信でFile書き込みのとき例外が発生する問題
				if(!(fmt == -88 ||fmt == -87 )){
					int index = 3;
					do{
						index += getChData(d, dlen, index);
					} while(index<dlen);
				}
				
			}

			private int getChData(byte[] d, int dlen, int index) {
				int paclen = byteUtil.valueOf2Byte(d,index);
				time = new wintime(d,5);
				// chdata = new channelRawData(d,11);
				index += 8;
				int plen = paclen-8;
				do{
					channelRawData rd = new channelRawData(d,index); 
					chdata.add(rd);
					index += (rd.DataLength + 4); 
					plen -= (rd.DataLength + 4);;
				} while(plen>0);
				return paclen;
			}
		
//			public int getChannelName(){
//				return chdata.getChannelName();
//			}
			public int[] getChannelName(){
				int num = chdata.size();
				int[] ret = new int[num];
				for(int i=0;i<num;i++){
					channelRawData rd = chdata.get(i);
					ret[i] = rd.getChannelName();
				}
				return ret;
			}
			
//			public channelRawData getChannelRawData(){
//			return chdata;
//		}
			public channelRawData[] getChannelRawData(){
				int num = chdata.size();
				channelRawData[] ret = new channelRawData[num];
				for(int i=0;i<num;i++){
					channelRawData rd = chdata.get(i);
					ret[i] = rd;
				}
				return ret;
			}
			public wintime getWinTime(){
				return time;
			}
			public int getWinTimeKey(){
				return time.getValueS();
			}
			public int getA0(){
				return fmt;
			}
			
		}
//		public class SecondBlock {
//			private ArrayList<ChannelData> ChDataList;
//			private wintime	Time;
//			private int BlockLength;
//			SecondBlock(byte[] c, int offset){
//				BlockLength = byteUtil.valueOf4Byte(c,0);
//				offset += 4;
//				int len=BlockLength;
//				len -= 4;
//				
//				Time = new wintime(c,offset);
//				offset += Time.length();
//				len -= Time.length();
//				ChDataList = new ArrayList<ChannelData>();
//				while(len>0){
//					ChannelData d = new ChannelData(c,offset,Time);
//					ChDataList.add(d);
//					offset += d.length();
//					len -= d.length();
//				}
//				assert(len==0);
//			}
//			public int length(){
//				return BlockLength;
//			}
//			public wintime getWinTime(){
//				return Time;
//			}
//			public ArrayList<ChannelData> getChannelDataList(){
//				return ChDataList;
//			}
//		}	
//		
//		public class channelData {
//			private int ChannelName;
//			private int samplerate;
//			private int encode;
//			private int[] Data;
//			private int DataLength;
//			private wintime Time;
//			private int min;
//			private int max;
//			
//			public int getChannelName() {
//				return ChannelName;
//			}
//			public int[] getData() {
//				return Data;
//			}
//			public int getEncode() {
//				return encode;
//			}
//			public int getSamplerate() {
//				return samplerate;
//			}
//			public int length(){
//				return 4 + DataLength;
//			}
//
//
//			public wintime getTime(){
//				return Time;
//			}
//			public int getMax(){
//				return max;
//			}
//			public int getMin(){
//				return min;
//			}
//			channelData(byte[] c,int offset,wintime tm){
//				Time = tm;
//				int index = offset;
//				// channel header decode
//				ChannelName = unsigned_byte.convShort(c[index])*0x100 + unsigned_byte.convShort(c[index+1]);
//				encode = (0xf0 & (unsigned_byte.convShort(c[index+2])))>>4;
//				samplerate = 0xfff & (unsigned_byte.convShort(c[index+2])*0x100 + unsigned_byte.convShort(c[index+3]));
//				index += 4;
//				
//				// how many data  
//				if(encode == 0){	// 0.5byte 
//					DataLength = 4 + samplerate / 2;
//				}
//				else{	// 1byte,2byte,3byte,4byte
//					DataLength = 4 + (samplerate-1) * encode;
//				}
//				// win data decoding ...
//				Data = new int[samplerate];
//				int d = byteUtil.valueOf4Byte(c,index);
//				min = max = d;
//				index += 4;
//				
//				Data[0]=d;
//				boolean hi = true;
//				for(int i=1;i<samplerate;i++){
//					int delta;
//					switch(encode){
//					case 4:		// 4byte
//						delta = byteUtil.valueOf4Byte(c,index);
//						index += 4;
//						d += delta;
//						break;
//					case 3:		// 3byte
//						delta = byteUtil.valueOf3Byte(c,index);
//						index += 3;
//						d += delta;
//						break;
//					case 2:		// 2byte
//						delta = byteUtil.valueOf2Byte(c,index);
//						index += 2;
//						d += delta;
//						break;
//					case 1:		// 1byte
//						delta = byteUtil.valueOf1Byte(c,index);
//						index += 1;
//						d += delta;
//						break;
//					case 0:		// 0.5byte
//						delta = byteUtil.valueOfHalfByte(c,index,hi);
//						if(hi){
//							hi = false;
//						}
//						else{
//							hi = true;
//							index += 1;
//						}
//						d += delta;
//						break;
//					}
//					Data[i]=d;
//					if(min > d) min = d;
//					if(max < d) max = d; 
//					
//				}
//			}
//
//		}	
	}
	
}


